package de.fh.ka.as.car.drivecontrol;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import de.fh.ka.as.car.R;
import de.fh.ka.as.car.R.drawable;
import de.fh.ka.as.car.drivecontrol.model.Car;
import de.fh.ka.as.car.drivecontrol.model.Control;
import de.fh.ka.as.car.gui.ActionEvent;
import de.fh.ka.as.car.gui.ActionListener;
import de.fh.ka.as.car.gui.Button;
import de.fh.ka.as.car.gui.GUIComponent;
import de.fh.ka.as.car.gui.Slider;
import de.fh.ka.as.car.gui.Slider.Orientation;

public class CanvasView extends View implements OnTouchListener, ActionListener {

	private List<GUIComponent> guiComponents = new LinkedList<GUIComponent>();

	private Button brake;
	private Button horn;
	private Button lightLeft, lightRight;
	private Slider speed;
	private Slider drive;

	private Car car;

	private Control control = new Control();
	private int pointerCount;
	private final int MAX_POINTERS = 25;

	public CanvasView(Context context, Car car) {
		super(context);
		this.car = car;
		speed = new Slider(10, 10, 320, 100, 0, 1, 0, Orientation.Vertical);
		drive = new Slider(460, 150, 120, 300, -1, 1, 0, Orientation.Horizontal);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pedal);
		brake = new Button(220, 250, 200, 90, bitmap);
		horn = new Button(150, 10, 90, 90);

		lightLeft = new Button(300, 10, 50, 50);
		lightLeft.setBackgroundColor(Color.YELLOW);

		lightRight = new Button(380, 10, 50, 50);
		lightRight.setBackgroundColor(Color.YELLOW);

		guiComponents.add(brake);
		guiComponents.add(speed);
		guiComponents.add(drive);
		guiComponents.add(horn);
		guiComponents.add(lightLeft);
		guiComponents.add(lightRight);

		brake.addActionListener(this);
		speed.addActionListener(this);
		drive.addActionListener(this);
		horn.addActionListener(this);
		lightLeft.addActionListener(this);
		lightRight.addActionListener(this);
		setOnTouchListener(this);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		for (GUIComponent component : guiComponents) {
			component.draw(canvas);
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction() & MotionEvent.ACTION_MASK;
		int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
		pointerCount = event.getPointerCount();

		int actionId = event.getPointerId(pointerIndex);

		// if (actionId < MAX_POINTERS) {
		// //lastActions[actionId] = action;
		// }

		for (int i = 0; i < pointerCount; i++) {
			int pointerId = event.getPointerId(i);
			if (pointerId < MAX_POINTERS) {
				// points[pointerId] =
				PointF point = new PointF(event.getX(i), event.getY(i));
				for (GUIComponent component : guiComponents) {
					component.touchEvent((int) point.x, (int) point.y, action);
				}
				if (action == MotionEvent.ACTION_MOVE) {
					// lastActions[pointerId] = action;
				}
			}
		}

		invalidate();
		return true;
	}

	@Override
	public void actionEventPerformed(ActionEvent e) {
		if (e.getSource().equals(brake)) {
			speed.setTouchEnabled(!brake.isPressed());
			// } else if (e.getSource().equals(speed)) {
		} else if (e.getSource().equals(drive)) {
			control.setSteering(drive.getValue());
		} else if (e.getSource().equals(horn)) {
			control.setHorn(horn.isPressed());
		}
		control.setBrake(brake.isPressed());

		if (brake.isPressed()) {
			speed.setValue(0);
			control.setSpeed(0);
		} else {
			control.setSpeed(speed.getValue());
		}

		if (e.getSource().equals(lightLeft)) {
			control.setLightLeft(lightLeft.isPressed());
		}
		if (e.getSource().equals(lightRight)) {
			control.setLightRight(lightRight.isPressed());
		}
		Log.i("GUI", control.toString() + "");
		car.control(control);

	}

	public Button getHorn() {
		return horn;
	}

}
